package com.central.apps.controller;


import cn.hutool.core.map.MapUtil;
import com.central.app.service.HandlerService;
import com.central.apps.service.OnlineEditService;
import com.central.apps.util.FileUtil;
import com.central.common.util.ToolUtil;
import com.central.redis.util.RedisUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "合同文档一键下载接口")
@CrossOrigin
@Controller
@RequestMapping("/file")
public class ConFileDownController {
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private HandlerService handlerService;
    @RequestMapping(value = "/downloadFiles", method = RequestMethod.GET)
    public void downloadFile(HttpServletRequest request, HttpServletResponse response,
                             @RequestParam("id") String id,@RequestParam("conname") String conname) throws Exception {

        //Map<String, String> fileData = onlineEditService.getFilePath(fileid, "conaccountApp");
        StringBuffer paths = new StringBuffer();
        String zipPath = "";
        StringBuffer filerealnames = new StringBuffer();
        try {
            List<Map<String, Object>> fileList = handlerService.findBySql("select * from tb_file where appname like '%accountApp%' and recordid =" + id + " and isshow=1");
            for (int i=0;i<fileList.size();i++) {
                String pathname = MapUtil.getStr(fileList.get(i), "filepath");
                String pathid = MapUtil.getStr(fileList.get(i), "pathid");
                // 文件原名称
                String filerealname = MapUtil.getStr(fileList.get(i), "filerealname");
                String filename = MapUtil.getStr(fileList.get(i), "filename");
                String extensionname = MapUtil.getStr(fileList.get(i), "extensionname");//文件后缀名
                // 文件md5值
                String md5 = MapUtil.getStr(fileList.get(i), "md5");
                String fileUserId = MapUtil.getStr(fileList.get(i), "userid");
                String fileUserName = MapUtil.getStr(fileList.get(i), "username");
                List<Map<String, Object>> pathList = handlerService.findBySql("select * from tb_path where pathid =" + pathid + "");
                String filepath = MapUtil.getStr(pathList.get(0), "pathname");
                String parentname = MapUtil.getStr(pathList.get(0), "parentname");
                zipPath = filepath;
//                if (StringUtils.isNotBlank(pathname)) {
//                    String[] split = pathname.split("/");
//                    pathname = "/" + split[split.length - 1];
//                }
                //String pt = filepath + pathname;
                filepath = parentname +pathname;
                paths.append(filepath).append(",");
                if(redisUtil.hasKey(filerealname)){
                    int index = (int) redisUtil.get(filerealname);
                    redisUtil.set(filerealname,index+1,200);
                }else {
                    redisUtil.set(filerealname,1,200);
                }
                if(filerealnames.toString().contains(filerealname)){
                    filerealnames.append(filename+"("+((int)redisUtil.get(filerealname)-1)+")"+"."+extensionname).append(",");
                }else {
                    filerealnames.append(filerealname).append(",");
                }

            }

            FileUtil.downLoadFileAll(  response,request,conname,paths.toString(),zipPath,filerealnames.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
